package com.spring.demo.web.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.spring.demo.util.Constant;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spring.demo.model.user.User;
import com.spring.demo.model.vo.ListView;
import com.spring.demo.model.vo.ResponseVo;
import com.spring.demo.web.BaseController;

@Controller
@RequestMapping(value = "/user")
public class UserController extends BaseController {

	@RequestMapping(value="/helloUser",method=RequestMethod.GET)
	public String helloUser(String userName){
		List<User> lsit = userService.getUser();
		//return "helloUser";
		//return "index";
		return "user/modal";
	}
	
	@RequestMapping(value="/selectAllUser",method=RequestMethod.GET)
	@ResponseBody
	public List<User> selectAllUser(){
		//List<User> list = userService.getUser();
		List<User> list = userService.testMybatis();
		return list;
	}
	@RequestMapping(value = "/userlist",method=RequestMethod.GET)
	public String toUserList(HttpServletRequest request){
		System.out.println("toUserList");
		//request.getAttribute(Constant.LOGIN_USER);
		User user = (User) request.getSession().getAttribute(Constant.LOGIN_USER);
		request.setAttribute("user", user);
		return "user/user_list";
	}
	
	@RequestMapping(value = "/ajaxUserList",method = RequestMethod.POST)
	@ResponseBody
	public ListView<User> ajaxUserList(String limit,String offset){
		List<User> list = userService.getUser();
		return new ListView<User>(1L, list);
	}
	
	@RequestMapping(value = "/ajaxSelectUserById",method = RequestMethod.GET)
	@ResponseBody
	public User ajaxSelectUserById(Long id){
		List<User> list = userService.selectUser(null,id);
		if(list!=null&&list.size()>0){
			return list.get(0);
		}else{
			return new User();
		}
	}
	
	@RequestMapping(value="/checkUserName",method=RequestMethod.GET)
	@ResponseBody
	public ResponseVo<Boolean> checkUserName(String userName){
		if(userName!=null&&!("".equals(userName))){
			List<User> list = userService.selectUser(userName,null);
			if(list!=null&&list.size()>0){
				responseVo = new ResponseVo<Boolean>("4000", "用户名已存在", false);
			}else{
				responseVo = new ResponseVo<Boolean>("5000", "验证通过", true);
			}
		}else{
			responseVo = new ResponseVo<Boolean>("4000", "用户名为空", false);
		}
		return responseVo;
	}
	
	@RequestMapping(value="registerUser",method=RequestMethod.POST)
	@ResponseBody
	public ResponseVo<Boolean> registerUser(User user){
		
		
		/*user.setPassWord("123");
		user.setType(2);
		user.setUserName("123");*/
		user.setDr(0);
		if(user.getPassWord()!=null){  //密码加密
			BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
			user.setPassWord(passwordEncoder.encode(user.getPassWord()));
			//System.out.println(passwordEncoder.encode(user.getPassWord()));
		}
		//user.setLeader(leader);
		boolean boo = userService.registerUser(user);
		if(boo){
			return new ResponseVo<Boolean>("5000", "success", true);
		}else{
			return new ResponseVo<Boolean>("4000", "fail", false);
		}
		
	}
	
	/*@RequestMapping(value="login",method=RequestMethod.GET)
	public String login() {
		return "login";
	}
	
	@RequestMapping(value="login",method=RequestMethod.POST)
	@ResponseBody
	public ResponseVo<User> login(User user){
		
		System.out.println(userName+"  "+passWord);
		User user = new User();
		user.setUserName(userName);  
		user.setPassWord(passWord);
		return new ResponseVo<User>("5000", "success" , user);
	}
	
	@RequestMapping(value="login2",method=RequestMethod.POST)
	public String login2(String userName,String passWord,HttpServletRequest request) {
		User userTemp = userService.loginUser(userName,passWord);
		System.out.println(userName+" "+passWord);
		if(userTemp!=null){
			request.getSession().setAttribute("user", userTemp);
			return "index";
		}else{
			request.setAttribute("errMessage", "qwe");
		}
		return "login";
		return "index";
	}*/
	
}
